﻿using HalconDotNet;
using Halcon二次开发.Model;
using Halcon二次开发.Model.自定义控件;
using Halcon二次开发.Tools;
using Halcon二次开发.View.卡尺;
using Halcon二次开发.View.多文本显示工具;
using Halcon二次开发.View.找圆_线_矩形;
using Halcon二次开发.View.文本显示工具;
using Halcon二次开发.View.距离转换工具;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using System.Windows.Forms;

namespace Halcon二次开发.Controller.多文本显示
{
    public class ShowTextController
    {
        public HSmartWindowControl hw;

        //输入的区域图像
        public HObject HideIntputImage;
        //输入的卡尺行坐标 1 
        public double[] HideIntputArrayRows;
        //输入的卡尺列坐标 2  
        public double[] HideIntputArrayColumns;
        //输入的距离转换长度 3 
        public double[] HideIntputArrayDistances;
        //输入的结果分析 4
        public List<bool> HideIntputResults = new List<bool>();
        //窗体的对象
        public MessageBoxStringsForms form;
        public HwindowFlowNodeUrseControl hwindowFlowNode;
        public ShowTextController(MessageBoxStringsForms form, HwindowFlowNodeUrseControl hwindowFlowNode)
        {
            //获取当前工位的HSmartWindowControl对象
            hw = hwindowFlowNode.hSmartWindowControl1;
            this.hwindowFlowNode = hwindowFlowNode;
            this.form = form;
        }

        public ShowTextController()
        {

        }

        /// <summary>
        /// 工具的单次运行
        /// </summary>
        public void Tool_Run()
        {
            if (HideIntputImage == null)
            {
                throw new Exception("当前图像为空");
            }
            hw.HalconWindow.ClearWindow();
            HOperatorSet.DispObj(HideIntputImage, hw.HalconWindow);
            Run();
        }


        /// <summary>
        /// 单次运行
        /// </summary>
        public void Run()
        {
            //显示在目标中心 靠右
            try
            {
                 if (HideIntputArrayRows == null || HideIntputArrayRows.Length == 0) return;
                    for (int i = 0; i < HideIntputArrayRows.Length; i++)
                    {
                        //设置文本显示的坐标
                        HOperatorSet.SetTposition(hw.HalconWindow, HideIntputArrayRows[i] + (double)form.numericUpDown2.Value, HideIntputArrayColumns[i] + (double)form.numericUpDown1.Value);
                        //设置字体
                        // HOperatorSet.SetFont(hw.HalconWindow,"宋体"+ "-Normal-" + form.textBox4.Text.Trim());
                        HOperatorSet.SetFont(hw.HalconWindow, $"宋体-Bold-{form.textBox4.Text.Trim()}");
                    if (HideIntputResults != null && HideIntputResults.Count != 0 && HideIntputResults[i])
                        {
                            //设置颜色
                            HOperatorSet.SetColor(hw.HalconWindow, form.comboBox2.Text);
                        }
                        else if (HideIntputResults != null && HideIntputResults.Count != 0 && !HideIntputResults[i])
                        {
                            //设置颜色
                            HOperatorSet.SetColor(hw.HalconWindow, form.comboBox3.Text);
                        }
                        else
                        {
                            //设置颜色
                            HOperatorSet.SetColor(hw.HalconWindow, "orange");
                        }
                        //设置文字
                        HOperatorSet.WriteString(hw.HalconWindow, form.textBox1.Text + HideIntputArrayDistances[i].ToString("F3"));
                    }
            }
            catch { 
                throw new Exception("当前计算机不支持该字体！\r\n");  
            }
        }

        public static void HardRun(MessageBoxStringsForms form,  double[] HideIntputArrayRows, double[] HideIntputArrayColumns, HWindowContentManager drawData,    double[] HideIntputArrayDistances,bool[] HideIntputResults)
        {
            if (HideIntputResults != null && HideIntputResults.Length > 100)
            {
                throw new Exception("当前显示的文本超过上限！\r\n");
            }
            try
            {
                if (HideIntputArrayRows == null || HideIntputArrayRows.Length == 0) return;
                for (int i = 0; i < HideIntputArrayRows.Length; i++)
                {
                    if (HideIntputResults != null && HideIntputResults.Length != 0 && HideIntputResults[i])
                    {

                        drawData.AddText(form.textBox1.Text + HideIntputArrayDistances[i].ToString("F3"),new HTuple(HideIntputArrayRows[i] + (double)form.numericUpDown2.Value, HideIntputArrayColumns[i] + (double)form.numericUpDown1.Value), form.comboBox2.Text,int.Parse( form.textBox4.Text));
                    }
                    else if (HideIntputResults != null && HideIntputResults.Length!= 0 && !HideIntputResults[i])
                    {

                        drawData.AddText(form.textBox1.Text + HideIntputArrayDistances[i].ToString("F3"), new HTuple(HideIntputArrayRows[i] + (double)form.numericUpDown2.Value, HideIntputArrayColumns[i] + (double)form.numericUpDown1.Value), form.comboBox3.Text, int.Parse(form.textBox4.Text));
                    }
                    else
                    {
                                                              
                    }
                }
            }
            catch
            {
                throw new Exception("当前计算机不支持该字体！\r\n");
            }
        }
    }
}
